<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
// import { dashExamWeek } from '@/api/stat/stat'

export default {
  name: 'StateChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    // dashExamWeek().then(res => {
    //   this.parseData(res.data)
    // })
    this.demo()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    demo() {
      const option = {
        xAxis: {
          type: 'category',
          data: ['开放客户', '签约客户', '保留客户']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: [120, 200, 150],
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          }
        }]
      }

      this.initChart(option)
    },

    // 构建统计数据
    parseData(data) {
      const dateList = []
      const examCountList = []
      const examUserList = []
      const passUserList = []

      for (let i = 0; i < data.length; i++) {
        const stat = data[i]
        dateList.push(stat.dateStr)
        examCountList.push(stat.examCount)
        examUserList.push(stat.examUser)
        passUserList.push(stat.passUser)
      }

      const option = {
        title: {
          text: '用户总览'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['考试人次', '考试人数', '通过人数']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: dateList
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '考试人次',
            type: 'line',
            data: examCountList
          },
          {
            name: '考试人数',
            type: 'line',
            data: examUserList
          },
          {
            name: '通过人数',
            type: 'line',
            data: passUserList
          }
        ]
      }

      this.initChart(option)
    },

    initChart(option) {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption(option)
    }
  }
}
</script>
